package fr.pud.client.controller.query;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.client.model.Time;
import fr.pud.client.view.QueryFrame;

/**
 * The controller which adds the query inside the singleton queries.
 * Apr 5, 2012 - tsp-osmBis
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes ConfirmAddingQuery.java
 */
public class CheckAddingQuery extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = 728077670779751223L;

    /**
     * Default constructor (sets the GUI up)
     */
    public CheckAddingQuery() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_QUERY));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VALIDATE_DESCRIPTION));
        this.putValue(SMALL_ICON, ImagesDatabase.PLUS);
    }

    /**
     * Get the information related to the query and adds it into the queries
     * singleton.
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        PUDStation pudStationPickUp = (PUDStation) QueryFrame.getInstance()
                .getDataPanel().getPUDStationPickUp().getSelectedItem();
        PUDStation pudStationDelivery = (PUDStation) QueryFrame.getInstance()
                .getDataPanel().getPUDStationDelivery().getSelectedItem();
        // Get the user inputs
        String textOpenPickUp = QueryFrame.getInstance().getDataPanel()
                .getTextOpeningTimePickup().getText();
        String textOpenDelivery = QueryFrame.getInstance().getDataPanel()
                .getTextOpeningTimeDelivery().getText();
        String textClosePickUp = QueryFrame.getInstance().getDataPanel()
                .getTextClosingTimePickup().getText();
        String textCloseDelivery = QueryFrame.getInstance().getDataPanel()
                .getTextClosingTimeDelivery().getText();
        String textQuantity = QueryFrame.getInstance().getDataPanel()
                .getTextQuantity().getText();
        String quantity = textQuantity;
        String textMaxDrive = QueryFrame.getInstance().getDataPanel()
                .getTextMaxDrive().getText();
        String maxDrive = textMaxDrive;
        String loadingTimePickUp = QueryFrame.getInstance().getDataPanel()
                .getTextLoadingTimePickUp().getText();
        String loadingTimeDelivery = QueryFrame.getInstance().getDataPanel()
                .getTextLoadingTimeDelivery().getText();
        // Check if the suer inputs are correct
        if (OpenQueryFrame.checkInputUser(quantity, maxDrive,
                loadingTimePickUp, loadingTimeDelivery, textOpenPickUp,
                textClosePickUp, textOpenDelivery, textCloseDelivery)) {
            Time timeOpenPickUp = new Time(textOpenPickUp);
            Time timeOpenDelivery = new Time(textOpenDelivery);
            Time timeClosePickUp = new Time(textClosePickUp);
            Time timeCloseDelivery = new Time(textCloseDelivery);
            // Modification of the delivery
            pudStationDelivery.setClosing(timeCloseDelivery);
            pudStationDelivery.setOpening(timeOpenDelivery);
            pudStationDelivery.setLoadingTime(Integer
                    .parseInt(loadingTimeDelivery));
            // Modification of the pickup
            pudStationPickUp.setClosing(timeClosePickUp);
            pudStationPickUp.setOpening(timeOpenPickUp);
            pudStationPickUp
                    .setLoadingTime(Integer.parseInt(loadingTimePickUp));
            // Creation of the query
            Query query = new Query(pudStationPickUp, pudStationDelivery,
                    Integer.parseInt(maxDrive), Integer.parseInt(quantity));
            Queries.getInstance().addQuery(query);
        }
        if (Queries.getInstance().getQueries().size() == 0) {
            QueryFrame.getInstance().getButtonModify().setVisible(false);
            QueryFrame.getInstance().getButtonDelete().setVisible(false);
        }
        else {
            QueryFrame.getInstance().getButtonModify().setVisible(true);
            QueryFrame.getInstance().getButtonDelete().setVisible(true);
        }
    }
}
